import React, { Component } from 'react';
import { Router, Route, hashHistory, IndexRedirect, browserHistory, IndexRoute } from 'react-router';
import { Layout, Menu, Breadcrumb, Icon, Modal ,message} from 'antd';
import Mutil from '../../utils/mm'
import MutilsCookie from '../../utils/cookie'
import Iframe from 'react-iframe';
import './style.less';
import Data from '../HeaderComponent/data.json';
import ERPfetch from '../../utils/Fetch/ERPfetch';
const { Sider } = Layout;
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;

// 获取url方法
function returnUrl(uri) {
    // console.log('uri',uri)
    // debugger
    var itemOneUrl = uri.split('/')
    var routerOne = itemOneUrl.splice(',', 4)//获取本地路由相对应的值
    // console.log(routerOne)
    var localhostRouter = routerOne[1]//二级菜单里面的url  channel
    // console.log(localhostRouter)
    var localhostKey = routerOne[routerOne.length - 1]//二级菜单里面的url   channekManagement
    // console.log("localhostKey",localhostKey)
    // debugger
    let localhostNO = '';
    if (localhostRouter == 'channel') {
        localhostNO = '3016';
    } else if (localhostRouter == 'product') {
        localhostNO = '3012';
    } else if (localhostRouter == 'purchase') {
        localhostNO = '3000';
    } else if (localhostRouter == 'order') {
        localhostNO = '3013';
    } else if (localhostRouter == 'stock') {
        localhostNO = '3019';
    } else if (localhostRouter == 'cms') {
        localhostNO = '3029';
    } else if (localhostRouter == 'task') {
        localhostNO = '3077';
    } else if (localhostRouter == 'settlement') {
        localhostNO = '3015';
    } else if (localhostRouter == 'ccc') {
        localhostNO = '3017';
    }
    // 判断是否为本地登录网址
    // let isLocalHost = (window.location.host == 'localhost:3018' ? true : false)
    // console.log('isLocalHost = ' + isLocalHost)
    // 本地:window.location.host == 'localhost:3018'
    // 测试环境:window.location.host == 'qaerp.dbuy.cn'
    // 正式环境:window.location.host == 'erp.dbuy.cn'
    // 项目交互
    // if (isLocalHost) {
    //     return `http://localhost:${localhostNO}/app/${localhostKey}`;
    // } else {
    //     return `http://qaerp.${localhostRouter}.dbuy.cn/app/${localhostKey}`;
    // }

    // 项目交互判断
    if (window.location.host == 'localhost:3018') {
        return `http://localhost:${localhostNO}/app/${localhostKey}`;
    } else if (window.location.host == 'qaerp.dbuy.cn') {
        return `http://qaerp.${localhostRouter}.dbuy.cn/app/${localhostKey}`; //测试环境
    } else if (window.location.host == 'erp.dbuy.cn') {
        return `http://erp.${localhostRouter}.dbuy.cn/app/${localhostKey}`; //正式环境
    }

    return '';
}
// function returnUrlSample(uri) {
//     // console.log(uri)
//     let isLocalHost = (window.location.host == 'localhost:3018' ? true : false)
//     // 项目交互
//     if (uri == 'sub11') {
//         return `http://localhost:3012/category`;
//     } else if (uri == 'sub21') {
//         return `http://localhost:3012/app/CommodityManagement`;
//     } else if (uri == 'sub31') {
//         return `http://localhost:3029/app/homeManagement`;
//     } else if (uri == 'sub32') {
//         return `http://localhost:3029/app/activityManagement`;
//     }
// }

// 链接数组去重
function unique(arr) {
    var res = [];
    var json = {};
    for (var i = 0; i < arr.length; i++) {
        if (!json[arr[i]]) {
            res.push(arr[i]);
            json[arr[i]] = 1;
        }
    }
    return res;
}
// 导航排序
function compare(prop) {
    return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    }
}


// 获取id
// let rootSubmenuKeysOne = [];
// function requestId(id){   
//     rootSubmenuKeysOne.push(id)
//     console.log(rootSubmenuKeysOne)
// }
// console.log(rootSubmenuKeysOne)
// debugger
export default class CustomSider extends Component {
    constructor(props) {
        super(props);
        // this.rootSubmenuKeysOne = ['1bb6a28b-5fca-47f1-bd7a-3bbc1f1d07a9','b185129d-8e7b-43a3-ae71-793cad1bf54a','sub5'],
        // this.rootSubmenuKeysOne = ['1f444a1b-d297-4814-9701-a02322173c49', '21a9e362-63ae-4ea8-9cb1-f36764040ae4', '308abd33-e1ee-4175-8a36-57e838976c60', '3abaf89e-d071-4323-ad42-91feb0b1e080', '5cf537e3-c4bd-46a5-99d2-6cdaf5c906a6', 'dea504d6-90ec-4274-a4a0-0dbbc4579e10'],
        // this.rootSubmenuKeysOne = ['sub1', 'sub2', 'sub3', 'sub4', 'sub5', 'sub6', 'sub7', 'sub8', 'sub9', 'sub10', 'sub11'];//一级菜单
        //this.rootSubmenuKeysTwo = ['sub51', 'sub52', 'sub53', 'sub54', 'sub55', 'sub61', 'sub61', 'sub63'];//二级菜单
        this.state = {
            token: Mutil.getStorage('userInfo').token,//获取token值localstorage
            tokenCookie: MutilsCookie.get('userInfoDb'),//cookie获取token值
            openKeys: [],//选取默认当前值
            siderDataOne: Mutil.getStorage('userInfo').resourceMenuDTO,//返回一级菜单//当前人物权限
            // siderDataTwo: Mutil.getStorage('userInfo').resourceMenuDTO ? Boolean(Mutil.getStorage('userInfo').resourceMenuDTO[0].childList) && Mutil.getStorage('userInfo').resourceMenuDTO[0].childList : null,//返回二级菜单
            // siderDataOne:[],
            // siderDataTwo:[],//返回二级菜单
            //rootSubmenuKeysOne : []//获取左边导航id
            //a:''//端口号
            //collapsed: false,//左边收放,
            subKeys: ['sub11', 'sub21', 'sub31', 'sub32', 'sub33', 'sub34', 'sub35', 'sub36'],
            rootSubmenuKeysOneOne: [],//左边栏一级导航id
            keyUrl: [],//url链接值
            siderDataTwo: [],//二级数组
            siderDataTwoChild: [],//
            siderDataThreeChild: [],//三级数组子级
            appCode: [],//模块选择
            sortArr: [],//序号排序
            getKey:"0",
            collapsed: false,


        };
    }
    onCollapse = (collapsed) => {
        console.log(collapsed);
        this.setState({ collapsed });
    }
    handleClick = (e) => {
        console.log('click ', e.key);
        if(e.key==="4"){
            browserHistory.push('/app/home/list')
        }else if(e.key==="5"){
            window.location.href='/app/home/add';
        }
    }

    componentWillMount() {
        // console.log(Data)
        // 缓存清除，左边栏数据为空是，判断是否返回到首页
        // if (Mutil.getStorage('userInfo').resourceMenuDTO == null) {
        //     browserHistory.push('/app/login')
        // }
        // 获取一级导航的id添加到数组里面
        let { siderDataOne, siderDataTwo, rootSubmenuKeysOneOne, keyUrl, sortArr } = this.state;
        // console.log(siderDataOne)
        siderDataOne&&siderDataOne.map(item => {
            // console.log(item.childList,item.sort)
            // 排序
            // sortArr.push(item.sort)
            // var compare = function (x, y) {//比较函数
            //     if (x < y) {
            //         return -1;
            //     } else if (x > y) {
            //         return 1;
            //     } else {
            //         return 0;
            //     }
            // }
            // console.log("sortSider",sortArr.sort(compare))
            // 左边id加入数组中
            rootSubmenuKeysOneOne.push(item.resourceId)
            // 把左边栏链接uri添加到数组
            // item.childList.map(itemUrl=>{
            //     keyUrl.push(itemUrl.uri)
            // })
            this.setState({
                rootSubmenuKeysOneOne,
                // keyUrl
            })
            // console.log('itemUrl',keyUrl)
            // console.log(rootSubmenuKeysOneOne)
        })
        // 一级排序
        siderDataOne = siderDataOne&&siderDataOne.sort(compare("sort"))
        this.setState({
            siderDataOne
        })
        // console.log(siderDataOne.sort(compare("sort")))
        // console.log(siderDataOne)
        // siderDataOne.map(item=>{
        //     console.log(item.sort)
        // })

        // let siderData = Mutil.getStorage('userInfo').AuthMenu[0].ChildList;//返回二级菜单
        // var a = this.state.a
        // siderData.map(item=>{
        //     return (
        //         item.ChildList.map(itemOne=>{
        //             var itemOneUrl = JSON.stringify(itemOne.uri).split('/')
        //             var routerOne = itemOneUrl.splice(',',2)//获取本地路由相对应的值
        //             var localhostRouterOne = routerOne[1]//二级菜单里面的url
        //             //var a = this.state.a//端口号
        //             var one = 'purchase'//采购
        //             var two = 'channel'//渠道
        //             var router = function () {
        //                 if(localhostRouterOne == one){
        //                     a = 3000
        //                 }else if(localhostRouterOne == two){
        //                     a = 3016
        //                 }
        //                 console.log('itemOne:'+localhostRouterOne,a)
        //             }
        //             return (
        //                 itemOne.ChildList==null?
        //                     router()
        //                 :itemOne.ChildList.map(itemTwo=>{
        //                     var itemTwoUrl1 = JSON.stringify(itemTwo.uri).split('/')
        //                     var routerTwo = itemTwoUrl1.splice(',',2);
        //                     var localhostRouterTwo = routerTwo[1]//获取本地路由相对应的值
        //                     // debugger
        //                     if(localhostRouterTwo == one){
        //                         a = 3000
        //                     }else if(localhostRouterTwo == two){
        //                         a = 3016
        //                     }
        //                     console.log('itemTwoUrl1:'+localhostRouterTwo,a)//三级菜单里面的url
        //                 })
        //             )
        //         })
        //     )
        //     console.log('65:'+a)
        // this.setState({
        //     a
        // })
        // })
        //console.log(`siderData=${JSON.stringify(siderData)}`)//返回二级菜单
    }
    render() {
        let { siderDataOne, siderDataTwo, subKeys, siderDataTwoChild } = this.state;
        // 判断是否是本地还是发布地址
        let isLocalHost = (window.location.host == 'localhost:3018' ? true : false)
        return (
            <div>
                <Sider
                    collapsible
                    collapsed={this.state.collapsed}
                    onCollapse={this.onCollapse}
                >
                    <div className="logo">尚品运营中心</div>
                    <Menu
                        defaultSelectedKeys={['1']}
                        defaultOpenKeys={['sub1']}
                        mode="inline"
                        theme="dark"
                        inlineCollapsed={this.state.collapsed}
                        onClick={this.handleClick}
                    >
                        <SubMenu key="sub1" title={<span><Icon type="desktop" /><span>CMS</span></span>}>
                            <SubMenu key="sub3" title={'预售管理'}>
                                <Menu.Item key="4">预售列表</Menu.Item>
                                <Menu.Item key="5">添加预售</Menu.Item>
                            </SubMenu>
                        </SubMenu>
                    </Menu>
                </Sider>
            </div>
        )
    }
}